﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class MedCenters : System.Web.UI.Page
{
    protected int CurrentPage;
    protected string sPageTitle;
    NewsType pageType;
    int iCentersNum = 0;
    DataTable DTTopNews;

    protected void Page_Load(object sender, EventArgs e)
    {
        GetParameters();
        GetTopNews();
    }

    private void GetParameters()
    {
        pageType = (NewsType)Convert.ToInt32(Request.QueryString["type"]);
        if (pageType > 0)
            ViewState["pageType"] = pageType;

        switch (pageType)
        {
            case NewsType.news:
                sPageTitle = "Top Новости";
                break;
            case NewsType.popular:
                sPageTitle = "Top Популярные статьи";
                break;
            case NewsType.MedicalTourism:
                sPageTitle = "Top Медецинский туризм";
                break;
        }
    }

    private void GetTopNews()
    {
        Dal.arParams.Clear();
        Dal.arParams.Add((int)pageType);
        DTTopNews = Dal.ExecuteDBsp("Admin_GetTopByCategory", Dal.arParams);
        if (DTTopNews != null)
        {
            if (DTTopNews.Rows.Count > 0)
            {
                iCentersNum = DTTopNews.Rows.Count;
                gvTopNews.DataSource = DTTopNews;
                gvTopNews.DataBind();
            }
        }
    }


    protected void gvTopNews_ItemCommand(object source, CommandEventArgs e)
    {
        bool bChanged = false;
        Int32 rowID;
        Int32.TryParse(e.CommandArgument.ToString(), out rowID);
        Int32 rowIndex = 0;

        //if (Convert.ToBoolean(ViewState["pnlManageVisible"]))
        //{
        //    ViewState["pnlManageVisible"] = false;
        //    pnlManage.Controls.Clear();
        //    pnlManage.Style.Add("display", "none");
        //}
        if (DTTopNews != null)
        {
            for (Int16 i = 0; i < DTTopNews.Rows.Count; i++)
            {
                if (DTTopNews.Rows[i]["ID"].Equals(rowID))
                {
                    rowIndex = i;
                    break;
                }
            }

            switch (e.CommandName)
            {
                case "MoveUp":
                    if (rowIndex > 0)
                    {
                        DTTopNews.Rows[rowIndex - 1]["orderID"] = rowIndex;
                        DTTopNews.Rows[rowIndex]["orderID"] = rowIndex - 1;
                        bChanged = true;
                    }
                    break;
                case "MoveDown":
                    if (rowIndex < DTTopNews.Rows.Count - 1)
                    {
                        DTTopNews.Rows[rowIndex + 1]["orderID"] = rowIndex;
                        DTTopNews.Rows[rowIndex]["orderID"] = rowIndex + 1;
                        bChanged = true;
                    }
                    break;
            }
            if (bChanged)
            {
                string sXml = string.Empty;
                for (int iCount = 0; iCount < DTTopNews.Rows.Count; iCount++)
                {
                    sXml += string.Format("<c id=\"{0}\" ord=\"{1}\"/>", DTTopNews.Rows[iCount]["ID"], DTTopNews.Rows[iCount]["orderID"]);
                }
                sXml = "<cs>" + sXml + "</cs>";

                Dal.arParams.Clear();
                Dal.arParams.Add(sXml);
                Dal.ExecuteDBsp("Admin_UpdateTopNewsOrderID", Dal.arParams);

            }
        }
        GetTopNews();
        //Session["FilesOrder"] = dt;
    }

}
